package com.jxw.launcher.util;

import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import java.io.File;
import java.io.IOException;

/* loaded from: classes.dex */
public class SwitchWallpaperUtil {
    private static SwitchWallpaperUtil mInstance;
    private WallpaperManager wallpaperManager;

    private SwitchWallpaperUtil(Context context) {
        this.wallpaperManager = null;
        this.wallpaperManager = WallpaperManager.getInstance(context);
    }

    public static SwitchWallpaperUtil getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new SwitchWallpaperUtil(context);
        }
        return mInstance;
    }

    public void setWallpaper(Bitmap bitmap) {
        try {
            this.wallpaperManager.setBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void setWallpaper(Drawable drawable) {
        setWallpaper(drawableToBitmap(drawable));
    }

    public void setWallpaper(String str) {
        if (new File(str).exists()) {
            setWallpaper(BitmapFactory.decodeFile(str, null));
        }
    }

    private static Bitmap drawableToBitmap(Drawable drawable) {
        Bitmap createBitmap;
        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            if (bitmapDrawable.getBitmap() != null) {
                return bitmapDrawable.getBitmap();
            }
        }
        if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
            createBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
        } else {
            createBitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        }
        Canvas canvas = new Canvas(createBitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return createBitmap;
    }
}
